package com.hmsm.sys.daoImpl;


import java.io.Serializable;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import com.hmsm.server.base.BaseDao;
import com.hmsm.sys.dao.DeptInfoDao;
import com.hmsm.sys.po.DeptInfo;

@Repository
public class DeptInfoDaoImpl extends BaseDao<DeptInfo, Integer> implements DeptInfoDao {
	
	private String aliasName = "this_";
	
	/**
	 * 根据机构ID获取部门信息
	 * @param rtxId
	 * @return List<DeptInfo>
	 * @author zhangshuai
	 * @date 2013年7月9日 16:55:36
	 */
	@SuppressWarnings("unchecked")
	public List<DeptInfo> getDeptInfoByOrgId(final int orgId){
		String hql = " from DeptInfo where orgId = "+orgId + " and state = 1 order by deptNo";
		return this.getHibernateTemplate().find(hql);
	}
	
	/**
	 * 获取所有的部门信息
	 * @return
	 * @author 何杰 2013-2-21
	 */
	@SuppressWarnings("unchecked")
	public List<DeptInfo> getAllDept(){
		String hql = "from DeptInfo where state = 1 order by deptNo";
		return this.getHibernateTemplate().find(hql);
	}
	
	/**
	 * 根据腾讯通ID获取部门信息
	 * @param rtxId
	 * @return
	 * @author 何杰 2013-2-22
	 */
	@SuppressWarnings("unchecked")
	public DeptInfo getDeptByRtxId(final int rtxId){
		String hql = " from DeptInfo where idType = "+rtxId+" and state = 1 order by deptNo";
		List<DeptInfo> all =  this.getHibernateTemplate().find(hql);
		if(all.size()>0){
			return all.get(0);
		}
		return null;
	}
	
	/**
	 * 获取当前所有营业厅(有效)
	 * @param 
	 * @return
	 * @author luoc 2013-08-31
	 */
	public List<DeptInfo> getAllDeptInfoList(){
		
		String sql="SELECT * FROM dept_info WHERE state=1"; 
		List<DeptInfo> list= this.queryBySql(sql);
		return list;
	}
	
	/**
	 * 获取当前所有营业厅(包括无效)
	 * @param 
	 * @return
	 * @author luoc 2013-08-31
	 */
	public List<DeptInfo> getAllAndNoDeptInfoList(){
		
		String sql="SELECT * FROM dept_info WHERE type = 1 "; 
		List<DeptInfo> list= this.queryBySql(sql);
		return list;
	}
	
	@Override
	public boolean isUnique(DeptInfo entityVo, String propertyList) {
		Assert.hasText(propertyList);
		Criteria criteria = createCriteria(getPersistenceClass()).setProjection(Projections.rowCount());
		String[] nameList = propertyList.split(",");
		try {
			// 循环加入唯一列
			for (String name : nameList) {
				Object value = PropertyUtils.getProperty(entityVo, name);
				if (value == null) {
					criteria.add(Restrictions.isNull(name));
				} else {
					criteria.add(Restrictions.eq(name, value));
				}
			}

			// 以下代码为了如果是update的情况,排除entity自身.

			String idName = "pkId";

			// 取得entity的主键值
			Serializable id = entityVo.getPkId();

			// 如果id!=null,说明对象已存在,该操作为update,加入排除自身的判断
			if (id != null){
				criteria.add(Restrictions.not(Restrictions.eq(idName, id)));
			}

		} catch (Exception e) {
			ReflectionUtils.handleReflectionException(e);
		}
		return (Integer) criteria.uniqueResult() == 0;
	}
	
	/**
	 * 创建Criteria对象.
	 * 
	 * @param criterions
	 *            可变的Restrictions条件列表,见{@link #getQuery(String,Object...)}
	 */
	public Criteria createCriteria(Class<?> clazz) {
		return getSession().createCriteria(clazz, aliasName);
	}
	
}
